import { isIdCard, isPhone, isUnifyTheSCC, validateIsemail } from '@fl/utils/validate'
import { reactive } from 'vue'

export function useRule(vm?: any) {
    const validatorShenfzh = (rule, value, callback) => {
        if (!isIdCard(value))
            callback(new Error('请输入正确的身份证号'))
        else callback()
    }
    const validatorphonenumber = (rule, value, callback) => {
        if (!isPhone(value))
            callback(new Error('请输入正确的手机号'))
        else callback()
    }

    const validatorOrgPhone = (rule, value, callback) => {
        // TODO: 该正则有问题，待优化
        // if (!isMob(value))
        //     callback(new Error('请输入正确的企业联系电话'))
        // else callback()
        if (!value)
            callback(new Error('请输入正确的企业联系电话'))

        callback()
    }

    const validatorunifiedSocialCreditCode = (rule, value, callback) => {
        if (!isUnifyTheSCC(value)) {
            callback(new Error('请输入正确的统一社会信用代码'))
        }
        else {
            const { dmChange, isEdit, isNext } = vm.exposed

            if (isEdit.value) {
                callback()
            }
            else {
                if (isNext.value)
                    dmChange(value, callback)
                else
                    callback()
            }
        }
    }

    const rules = reactive<any>({
        catTypeId: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
        // enterpriseAccount: [
        //     { message: '请输入', required: true, trigger: 'blur' },
        // ],
        enterpriseLevelId: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
        enterpriseName: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
        enterpriseShort: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
        farzjhm: [
            { message: '请输入正确的证件号', required: true, trigger: 'blur', validator: validatorShenfzh },
        ],
        legalRepresentative: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
        legalRepresentativesfzfmurl: [
            { message: '请上传图片', required: true, trigger: 'blur' },
        ],
        orgPhone: [
            { required: true, trigger: 'blur', validator: validatorOrgPhone },
        ],
        phonenumber: [
            { required: true, trigger: 'blur', validator: validatorphonenumber },
        ],
        qyfrsfzzmurl: [
            { message: '请上传图片', required: true, trigger: 'blur' },
        ],
        shenfzh: [
            { required: true, trigger: 'blur', validator: validatorShenfzh },
        ],
        unifiedSocialCreditCode: [
            { required: true, trigger: 'blur', validator: validatorunifiedSocialCreditCode },
        ],
        userName: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
        yingyzzurl: [
            { message: '请上传图片', required: true, trigger: 'blur' },
        ],
        youx: [
            { required: false, trigger: 'blur', validator: validateIsemail },
        ],
        zhucrq: [
            { message: '请选择', required: true, trigger: 'blur' },
        ],
    })

    const userRules = reactive<any>({
        // email: [
        //     { required: true, trigger: 'blur', validator: validateIsemail },
        // ],
        enterpriseAccount: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
        phonenumber: [
            { required: true, trigger: 'blur', validator: validatorphonenumber },
        ],
        userName: [
            { message: '请输入', required: true, trigger: 'blur' },
        ],
    })

    // TODO：手机号 企业名称 唯一性校验
    const isOnly = (key: string, val: string) => {
        const cache = window.sessionStorage.getItem('CACHE_phonenumber_enterpriseName')
        const target: any = cache ? JSON.parse(cache) : ''
        return !target[key].includes(val)
    }

    const user = window.localStorage.getItem('user')
    const denglm = user ? JSON.parse(user).userInfo.code : ''

    return { denglm, isOnly, rules, userRules }
}
